package com.neueedu.android;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.IOException;
import java.io.InputStream;
import java.util.Hashtable;

public class ImageViewActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_view);

        ImageView ivQrCode = findViewById(R.id.ivQrCode);
        ImageView ivNetImage = findViewById(R.id.ivNetImage);


        createQRImage("https://www.baidu.com",ivQrCode,300,300);
        createNetImage("https://img0.baidu.com/it/u=721495486,2255853053&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=750",ivNetImage);

    }

    private void createNetImage(final String imageUrl, final  ImageView ivNetImage) {


        new AsyncTask<String,String,Bitmap>(){

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected Bitmap doInBackground(String... strings) {

                Bitmap bitmap = null;
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url(imageUrl).addHeader("User-Agent","").build();

                try {
                    Response response = client.newCall(request).execute();

                    bitmap = BitmapFactory.decodeStream(response.body().byteStream());


                } catch (IOException e) {
                    e.printStackTrace();
                }

                //加载网络
                return bitmap;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                super.onPostExecute(bitmap);
                ivNetImage.setImageBitmap(bitmap);
            }
        }.execute();





    }


    public static void createQRImage(String data, ImageView ivQrcode, int QR_WIDTH,  int QR_HEIGHT) {
        try {
            Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
            hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
            BitMatrix bitMatrix = new QRCodeWriter().encode(data, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints);
            int[] pixels = new int[QR_WIDTH * QR_HEIGHT];
            for (int y = 0; y < QR_HEIGHT; y++) {
                for (int x = 0; x < QR_WIDTH; x++) {
                    if (bitMatrix.get(x, y)) {
                        pixels[y * QR_WIDTH + x] = 0xff000000;
                    } else {
                        pixels[y * QR_WIDTH + x] = 0xffffffff;
                    }
                }
            }



            Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,  Bitmap.Config.ARGB_8888);
            bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);
            ivQrcode.setImageBitmap(bitmap);



        } catch (Exception e) {

        }



    }
}
